<template>
  <basic-container>
    <avue-crud :option="option"
               :table-loading="loading"
               :data="data"
               :page="page"
               :permission="permissionList"
               :before-open="beforeOpen"
               v-model="form"
               ref="crud"
               @row-update="rowUpdate"
               @row-save="rowSave"
               @row-del="rowDel"
               @search-change="searchChange"
               @search-reset="searchReset"
               @selection-change="selectionChange"
               @current-change="currentChange"
               @size-change="sizeChange"
               @refresh-change="refreshChange"
               @on-load="onLoad">
      <template slot="menuLeft">
        <!--<el-button type="danger"-->
                   <!--size="small"-->
                   <!--icon="el-icon-delete"-->
                   <!--plain-->
                   <!--v-if="permission.post_delete"-->
                   <!--@click="handleDelete">删 除-->
        <!--</el-button>-->
      </template>
      <template slot-scope="{row}"
                slot="deptId">
        <el-tag>{{row.deptName}}</el-tag>
      </template>
      <template slot-scope="{row}"
                slot="pcd">
        {{row.provinceName + ' ' + row.cityName + ' ' + row.districtName + ' ' + row.villageName }}
        <!--<el-tag>{{row.provinceName}}{{row.cityName}}</el-tag>-->
      </template>
    </avue-crud>
  </basic-container>
</template>

<script>
  import {getList, getDetail, add, update, remove} from "@/api/system/post";
  import {mapGetters} from "vuex";
  import website from "@/config/website";

  export default {
    data() {
      return {
        form: {
          text111 : 'http://cloud.sinyway.com/service/amap.html'
        },
        query: {},
        loading: true,
        page: {
          pageSize: 10,
          currentPage: 1,
          total: 0
        },
        selectionList: [],
        option: {
          height: 'auto',
          calcHeight: 210,
          tip: false,
          searchShow: true,
          searchMenuSpan: 6,
          border: true,
          index: true,
          viewBtn: true,
          selection: true,
          delBtn: false,
          viewBtn: false,
          menuWidth: 120,
          column: [
            {
              label: "所属组织",
              prop: "deptId",
              type: "select",
              search: false,
              dicUrl: "/api/blade-system/dept/tree",
              props: {
                label: "title",
                value: "id"
              },
              rules: [{
                required: true,
                message: "请选择所属组织",
                trigger: "blur"
              }],
              slot:true,
              span: 12,
            },
            {
              label: '省份',
              prop: 'provinceCode',
              type: 'select',
              search: true,
              props: {
                label: 'provinceName',
                value: 'provinceCode'
              },
              rules: [{
                required: true,
                message: "请选择所属省份",
                trigger: "blur"
              }],
              cascaderChange: true,
              cascaderItem: ['cityCode', 'districtCode','townCode','villageCode'],
              dicUrl: '/api/blade-system/region/select',
              span: 12,
              hide:true
            },
            {
              label: '地市',
              prop: 'cityCode',
              type: 'select',
              search: true,
              props: {
                label: 'cityName',
                value: 'cityCode'
              },
              rules: [{
                required: true,
                message: "请选择所属地市",
                trigger: "blur"
              }],
              defaultIndex: 1,
              dicFlag: false,
              dicUrl: '/api/blade-system/region/select?code={{key}}',
              span: 12,
              hide:true
            },
            {
              label: '区县',
              prop: 'districtCode',
              type: 'select',
              search: true,
              props: {
                label: 'districtName',
                value: 'districtCode'
              },
              rules: [{
                required: true,
                message: "请选择所属区县",
                trigger: "blur"
              }],
              defaultIndex: 1,
              dicFlag: false,
              dicUrl: '/api/blade-system/region/select?code={{key}}',
              span: 12,
              hide:true
            },
            {
              label: '镇',
              prop: 'townCode',
              type: 'select',
              search: true,
              props: {
                label: 'townName',
                value: 'townCode'
              },
              rules: [{
                required: true,
                message: "请选择所属镇",
                trigger: "blur"
              }],
              defaultIndex: 1,
              dicFlag: false,
              dicUrl: '/api/blade-system/region/select?code={{key}}',
              span: 12,
              hide:true
            },
            {
              label: '社区',
              prop: 'villageCode',
              type: 'select',
              search: false,
              props: {
                label: 'villageName',
                value: 'villageCode'
              },
              rules: [{
                required: true,
                message: "请选择所属社区",
                trigger: "blur"
              }],
              dicFlag: false,
              dicUrl: '/api/blade-system/region/select?code={{key}}',
              span: 12,
              hide:true
            },
            {
              label: "站点名称",
              prop: "siteName",
              search: true,
//              type: "select",
//              dicUrl: "/api/blade-system/site/select",
//              props: {
//                label: "siteName",
//                value: "id"
//              },
              rules: [{
                required: true,
                message: "请选择站点名称",
                trigger: "blur"
              }],
              span: 12,
            },
            {
              label: "站点类型",
              prop: "siteType",
              search: false,
              type: "select",
              dicUrl: "/api/blade-system/dict/dictionary?code=site_type",
              props: {
                label: "dictValue",
                value: "dictKey"
              },
              rules: [{
                required: true,
                message: "请选择站点类型",
                trigger: "blur"
              }],
              span: 12,
            },
            {
              label: "户号数",
              prop: "accountNumber",
              search: false,
              rules: [{
                required: false,
                message: "请输入手机号",
                trigger: "blur",
              }],
              span:6,
              addDisplay: false,
              editDisplay:false,
            },
            {
              label: '省区县',
              prop: 'pcd',
              search: false,
              addDisplay: false,
              editDisplay:false,
              slot:true,
              span:12
            },
            {
              label: "负责人",
              prop: "chargeman",
              search: false,
              rules: [{
                required: false,
                message: "请输入负责人",
                trigger: "blur",
              }],
              span:12,
            },
            {
              label: "手机号",
              prop: "phone",
              search: false,
              rules: [{
                required: false,
                message: "请输入手机号",
                trigger: "blur",
              }],
              span:12,
            },
            {
              label: "站点地址",
              prop: "address",
              search: false,
              rules: [{
                required: false,
                message: "请输入站点地址",
                trigger: "blur",
              }],
              span:12,
            },
            {
              label: "经度",
              prop: "longitude",
              search: false,
              rules: [{
                required: true,
                message: "请输入经度",
                trigger: "blur",
              }],
              hide:true,
              span:12,
              placeholder: '列如:114.07',
              tip: '请先到网站上查询经纬度再填',
            },
            {
              label: "维度",
              prop: "latitude",
              search: false,
              rules: [{
                required: true,
                message: "请输入维度",
                trigger: "blur",
              }],
              hide:true,
              placeholder: '列如:22.62',
              span:12,
              tip: '请先到网站上查询经纬度再填',
            },
            {
              label: "经纬度网站",
              type: '',
              prop: "text111",
              detail:true,
              search: false,
              hide:true,
              placeholder: 'http://cloud.sinyway.com/service/amap.html',
              span:12,
            },
          ]
        },
        data: []
      };
    },
    computed: {
      ...mapGetters(["permission"]),
      permissionList() {
        return {
          addBtn: this.vaildData(this.permission.post_add, false),
          viewBtn: this.vaildData(this.permission.post_view, false),
          delBtn: this.vaildData(this.permission.post_delete, false),
          editBtn: this.vaildData(this.permission.post_edit, false)
        };
      },
      ids() {
        let ids = [];
        this.selectionList.forEach(ele => {
          ids.push(ele.id);
        });
        return ids.join(",");
      }
    },
    methods: {
      rowSave(row, done, loading) {
        row.provinceName = row.$provinceCode
        row.cityName = row.$cityCode
        row.districtName = row.$districtCode
        row.townName = row.$townCode
        add(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowUpdate(row, index, done, loading) {
        update(row).then(() => {
          this.onLoad(this.page);
          this.$message({
            type: "success",
            message: "操作成功!"
          });
          done();
        }, error => {
          window.console.log(error);
          loading();
        });
      },
      rowDel(row) {
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(row.id);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
          });
      },
      handleDelete() {
        if (this.selectionList.length === 0) {
          this.$message.warning("请选择至少一条数据");
          return;
        }
        this.$confirm("确定将选择数据删除?", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        })
          .then(() => {
            return remove(this.ids);
          })
          .then(() => {
            this.onLoad(this.page);
            this.$message({
              type: "success",
              message: "操作成功!"
            });
            this.$refs.crud.toggleSelection();
          });
      },
      beforeOpen(done, type) {
        if (type === 'add') {
          console.log(this.form);
          this.form = {}
          this.form.text111 = 'http://cloud.sinyway.com/service/amap.html'
        }
        if (["edit", "view"].includes(type)) {
          getDetail(this.form.id).then(res => {
            this.form = res.data;
            this.form.text111 = 'http://cloud.sinyway.com/service/amap.html'
          });
        }
        done();
      },
      searchReset() {
        this.query = {};
        this.onLoad(this.page);
      },
      searchChange(params, done) {
        this.query = params;
        this.page.currentPage = 1;
        this.onLoad(this.page, params);
        done();
      },
      selectionChange(list) {
        this.selectionList = list;
      },
      selectionClear() {
        this.selectionList = [];
        this.$refs.crud.toggleSelection();
      },
      currentChange(currentPage) {
        this.page.currentPage = currentPage;
      },
      sizeChange(pageSize) {
        this.page.pageSize = pageSize;
      },
      refreshChange() {
        this.onLoad(this.page, this.query);
      },
      onLoad(page, params = {}) {
        this.loading = true;
        getList(page.currentPage, page.pageSize, Object.assign(params, this.query)).then(res => {
          const data = res.data;
          this.page.total = data.total;
          this.data = data.records;
          this.loading = false;
          this.selectionClear();
        });
      }
    }
  };
</script>

<style>
</style>
